package com.etrans.web.gps.business.areaTask.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.business.areaTask.domain.AreaAnalyzesVo;
import com.etrans.web.gps.business.areaTask.domain.OrganzationVO;
import com.etrans.web.gps.business.areaTask.service.AreaSendMessageService;
import com.etrans.web.gps.util.Config;

@Controller
@RequestMapping("/mail/area/*")
public class areaController {
	
	@Autowired
	private AreaSendMessageService areaSendMessageService;
	
	
	/**
	 * 跳转考核信息页面
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("areaAnalyzes.html")
	public void areaAnalyzes(HttpServletRequest request,HttpServletResponse response ,String year,String timeType,String detail,String areaId,
			String queryType,String queryTime) throws ParseException{
		try {
			response.setContentType("text/plain");  
		    response.setHeader("Pragma", "No-cache");  
		    response.setHeader("Cache-Control", "no-cache");  
		    response.setDateHeader("Expires", 0);  
		    response.setCharacterEncoding("utf-8");
			HashMap<String,Object> param = new HashMap<>();
			param.put("area_id", areaId);
			List<OrganzationVO> list = areaSendMessageService.queryAllAreaList(param);
			List<AreaAnalyzesVo> areaList=null;
			if ("week".equals(queryType)) {
				areaList = areaSendMessageService.queryAreaAnalyzesWeek(queryTime);
			}
			if ("month".equals(queryType)) {
				areaList = areaSendMessageService.queryAreaAnalyzesMonth(queryTime);
			}
			boolean flag = false;
			List<Map<String, Object>> result = areaSendMessageService.sendMail(list, areaList, detail, year, timeType,queryType,queryTime,flag);
			Map<String,Object> temp =result.get(0);
			if ("week".equals(queryType)) {
				temp.put("timeType","第"+timeType+"周");
			}else{
				temp.put("timeType",year+"年-"+timeType+"月");
			}
			temp.put("detail",detail);
			PrintWriter out = response.getWriter();       
			Object resultJSON = JSONObject.toJSON(result.get(0)); //根据需要拼装json  
			String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数  
			out.println(jsonpCallback+"("+resultJSON+")");//返回jsonp格式数据  
			out.flush();  
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
